import { defineConfig } from "vite";
import { resolve } from "path";
import vue from "@vitejs/plugin-vue";
import legacy from "@vitejs/plugin-legacy";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { NaiveUiResolver } from "unplugin-vue-components/resolvers";

// const pathResolve = (dir) => {
//   return resolve(process.cwd(), ".", dir);
// };

export default defineConfig({
  resolve: {
    alias: {
      "@": resolve(__dirname, "src"),
    },
    dedupe: ["vue"],
  },
  plugins: [
    vue(),
    legacy({
      targets: ["defaults", "IE 11"], // 兼容 IE11，也支持老 Safari
      additionalLegacyPolyfills: ["regenerator-runtime/runtime"],
      modernPolyfills: true,
    }),
    AutoImport({
      imports: [
        "vue",
        {
          "naive-ui": [
            "useDialog", // 弹窗
            "useMessage", // 消息提示
            "useNotification", // 通知
            "useLoadingBar", // 加载条
          ],
        },
      ],
    }),
    Components({
      resolvers: [NaiveUiResolver()], // 自动解析 Naive UI 组件
    }),
  ],
  server: {
    port: 9536,
    // hmr: true,
    proxy: {
      "/api": {
        target: "http://0.0.0.0:10180",
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, "/api"),
      },
      "/file": {
        target: "http://0.0.0.0:10180",
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/file/, ""),
      },
    },
  },
  // Vitest 专属配置
  test: {
    globals: true,
    environment: "happy-dom",
    coverage: {
      provider: "v8",
      reporter: ["text", "json", "html"],
    },
    setupFiles: "./tests/setup.js", // 测试初始化文件
  },
});
